package com.tianyang.yukuaifu.utils;

import android.text.TextUtils;

import androidx.annotation.Nullable;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Description :校验工具类
 * Created by 薛勇军 on 2018/6/12.
 */
public class VerifyUtil {

    public static boolean isEmpty(@Nullable CharSequence str) {
        if (str == null || str.length() == 0 || "0".equals(str))
            return true;
        else
            return false;
    }

    public static String decimalFormat(String decimal) {
        if (!TextUtils.isEmpty(decimal)) {
            DecimalFormat df = new DecimalFormat("0.00");
            return df.format(Double.parseDouble(String.valueOf(decimal)));
        }
        return "0.00";
    }

    public static String decimalFormat(BigDecimal decimal) {
        if (decimal != null) {
            DecimalFormat df = new DecimalFormat("0.00");
            return df.format(decimal);
        }
        return "0.00";
    }

    /**
     * 获取count个随机数
     *
     * @param count 随机数个数
     * @return
     */
    public static String random(int count) {
        StringBuffer sb = new StringBuffer();
        String str = "0123456789";
        Random r = new Random();
        for (int i = 0; i < count; i++) {
            int num = r.nextInt(str.length());
            sb.append(str.charAt(num));
            str = str.replace((str.charAt(num) + ""), "");
        }
        return sb.toString();
    }

    /**
     * 校验字符串是否为空，用于setTextView
     */
    public static String checkString(String string) {
       if (!TextUtils.isEmpty(string))
           return string;
       else
            return "";
    }

    /**
     * 验证手机号码
     *
     * @param phoneNumber 手机号码
     * @return boolean true 手机号码错误
     */
    public static boolean checkPhoneNumber(CharSequence phoneNumber) {
        Pattern pattern = Pattern.compile("^1[0-9]{10}$");
        Matcher matcher = pattern.matcher(phoneNumber);
        return !matcher.matches();
    }

    /**
     * 验证手机号码
     *
     * @param phoneNumber 手机号码
     * @return boolean true 手机号码错误
     */
    public static boolean checkPhoneNumber(CharSequence phoneNumber, boolean showToast) {
        if (TextUtils.isEmpty(phoneNumber)) {
            ToastUtil.showTextViewPromptLong("电话号码不能为空");
            return true;
        }

        Pattern pattern = Pattern.compile("^1[0-9]{10}$");
        Matcher matcher = pattern.matcher(phoneNumber);
        boolean matches = matcher.matches();
        if (showToast && !matches) {
            ToastUtil.showTextViewPromptLong("电话号码输入不正确");
        }
        return !matches;
    }

    /**
     * 验证银行卡号
     *
     * @param bankNumber 银行卡号
     * @return boolean
     */
    public static boolean checkBankNumber(String bankNumber) {
        if (TextUtils.isEmpty(bankNumber)) {
            ToastUtil.showTextViewPromptLong("银行卡号不能为空");
            return false;
        }
        return true;
    }

    /**
     * 验证身份证号是否符合规则
     *
     * @param text 身份证号
     * @return
     */
    public static boolean checkIdValidation(String text) {
        String regx = "[0-9]{17}x";
        String regx1 = "[0-9]{17}X";
        String reg1 = "[0-9]{15}";
        String regex = "[0-9]{18}";
        return !(text.matches(regx) || text.matches(regx1) || text.matches(reg1) || text.matches(regex));
    }

    public static String stringFilter(String str) throws PatternSyntaxException {
        // 只能输入 字母、数字、下划线和减号  最长 40
        String regEx = "[^a-zA-Z0-9-_]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    /**
     * 校验名字中不能出现特殊符号
     * @param nickName
     * @return
     */
    public static boolean checkName(String nickName) {
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern p = Pattern.compile(regEx);
        final Matcher m = p.matcher(nickName);
        if (m.find()) {
            return false;
        }
        return true;
    }

    /**
     * 验证是否是数字
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 实名名字***替换
     */
    public static String secretCrpName(String name) {
        try {
            if (!TextUtils.isEmpty(name)) {
                CharSequence start = name.subSequence(0, 1);
                CharSequence end = "";
                if (name.length()>2) {
                    end = name.subSequence(name.length()-1, name.length());
                }
                return start + "*" + end;
            }
            return name;
        } catch (Exception e) {
            e.printStackTrace();
            return name;
        }
    }

    /**
     * 电话号码***替换
     */
    public static String secretPhoneNumber(String number) {
        String secret = "";
        try {
            CharSequence start = number.subSequence(0, 3);
            CharSequence end = number.subSequence(number.length() - 4, number.length());

            return start + "****" + end;
        } catch (Exception e) {
            e.printStackTrace();
            return secret;
        }
    }

    /**
     * 银行卡号***替换
     */
    public static String secretBankCardNumber(String cardNumber) {
        try {
            CharSequence start = cardNumber.subSequence(0, 4);
            CharSequence end = cardNumber.subSequence(cardNumber.length() - 4, cardNumber.length());

            return start + "********" + end;
        } catch (Exception e) {
            e.printStackTrace();
            return cardNumber;
        }
    }

    /**
     * 银行卡尾号
     */
    public static String getBankCardNumber(String cardNumber) {
        try {
            return cardNumber.subSequence(cardNumber.length() - 4, cardNumber.length()) + "";
        } catch (Exception e) {
            e.printStackTrace();
            return cardNumber;
        }
    }

    /**
     * 判断是否包含中文
     */
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否包含中文
     */
    public static boolean isDifferenceNum(String edit, String min, String max) {
        if (!TextUtils.isEmpty(edit) && !TextUtils.isEmpty(min) && !TextUtils.isEmpty(max)) {
            if (new BigDecimal(edit).compareTo(new BigDecimal(min)) < 0) {
                return false;
            }
            return new BigDecimal(edit).compareTo(new BigDecimal(max)) <= 0;
        }
        return true;
    }
}
